﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace HomeWork10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            String url = "http://www.baidu.com/s?tn=news&wd=" + textBox1.Text+"&ie=utf-8";
            String result = await Test(url);
            MatchCollection c1 = Regex.Matches(result, @"""absAriaLabel"":"".*?""");
            var textbox = richTextBox1;
            int sumlength = 0;
            foreach (Match match in c1)
            {
                String temp = match.ToString();
                temp = temp.Substring(19, temp.Length - 19);
                textbox.Text += temp + Environment.NewLine;
                if (textbox.TextLength >= 100)
                {
                    textbox = richTextBox2;
                }
            }
        }

        static Task <String> Test(String url)
        {
            return Task<String>.Run(() => {
                WebRequest request = WebRequest.Create(url);//获取页面内容
                request.Timeout = 200000;//20秒超时 
                WebResponse response = request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream);
                string tempstr = sr.ReadToEnd();//获取页面html代码
                return tempstr;
            });

        }

    }
}
